/**
 * @file MainForm
 *
 * @brief 
 *
 * @author gyzheng
 *
 * @mail gyazheng@gmail.com
 *
 * @date 07/27/2011 22:37:51 
 *
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CarFrameWork.Model;
using CarFrameWork.Common;

namespace CarFrameWork.UI.SystemManage
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            InitData();
            LoadINIFile();
            string backImagePath = Application.StartupPath + @"\background.jpg";
            //MessageBox.Show(backImagePath);
            if (File.Exists(backImagePath))
            {
                this.BackgroundImage = System.Drawing.Image.FromFile(backImagePath);
                this.ClientSize = new System.Drawing.Size(this.BackgroundImage.Size.Width,this.BackgroundImage.Size.Height);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string loginName = txtName.Text;
            string password = txtPassword.Text;

            UserInfo userInfo = new UserInfo();
            string message = userInfo.Login(loginName, password);
            //string message = userInfo.Login(loginName, password);
            
            //if (string.IsNullOrEmpty(message))
            if (string.IsNullOrEmpty(message))
            {
                this.Hide();
                Global.User_Name = loginName;
                Global.User_Permissions = userInfo.GetRoleId(loginName);
                Global.User_Group = DbHelperSQL.Query("Select GroupName From GroupInfo Where ID = "+ userInfo.GetGroupId(loginName).ToString()).Tables[0].Rows[0][0].ToString();
                MainForm.Current.ShowDialog();
                this.Close();
            }
            else
            {
                errorProviderUser.SetError(txtPassword, message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(this, null);
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            this.errorProviderUser.Clear();
        }

        private void InitData()
        {
            ParameSystem parameSystem = new ParameSystem();
            if (parameSystem.Exists())
            {
                Global.CAE_Path = parameSystem.caePath;
                Global.UG_Path = parameSystem.ugPath;
                Global.Ftp_IP = parameSystem.ftpIp;
                Global.Ftp_UserName = parameSystem.ftpUserName;
                Global.Ftp_PassWord = parameSystem.ftpPassWord;
            }
        }

        private void LoadINIFile()
        {
            IniHelper ini = new IniHelper();    
            ini.InitPath(@".\Config.ini");
            Global.DataBase_LinkString =ini.IniReadValue("DataBase", "LINKSTRING");
        }
    }
}